﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Controls.Primitives;

namespace EnsureWeb.Controls
{
    public class DataFormPanel : ContentControl
    {
        #region
        public object Title
        {
            get { return (object)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(object), typeof(DataFormPanel), new PropertyMetadata("DataFormPanel"));

        // Using a DependencyProperty as the backing store for Cancel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CancelProperty =
            DependencyProperty.Register("Cancel", typeof(ICommand), typeof(DataFormPanel), new PropertyMetadata(null));

        public bool IsShow
        {
            get { return (bool)GetValue(IsShowProperty); }
            set { SetValue(IsShowProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsShow.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShowProperty =
            DependencyProperty.Register("IsShow", typeof(bool), typeof(DataFormPanel), new PropertyMetadata(true, OnIsShowChanged));

        private static void OnIsShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var isShow = (bool)e.NewValue;
            ScaleTransform scale = new ScaleTransform() { ScaleX = isShow ? 1 : 0 };

            var self = d as DataFormPanel;
            self.RenderTransform = scale;
        }

        public ICommand SubmitCommand
        {
            get { return (ICommand)GetValue(SubmitCommandProperty); }
            set { SetValue(SubmitCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SubmitCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SubmitCommandProperty =
            DependencyProperty.Register("SubmitCommand", typeof(ICommand), typeof(DataFormPanel), new PropertyMetadata(null));

        public ICommand CancelCommand
        {
            get { return (ICommand)GetValue(CancelCommandProperty); }
            set { SetValue(CancelCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CancelCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CancelCommandProperty =
            DependencyProperty.Register("CancelCommand", typeof(ICommand), typeof(DataFormPanel), new PropertyMetadata(null));
    
        #endregion
        
        public DataFormPanel(): base()
        {
            this.DefaultStyleKey = typeof(DataFormPanel);           
        }

        public override void OnApplyTemplate()
        { 
            base.OnApplyTemplate();

            ButtonBase bgButton = this.GetTemplateChild("bgButton") as ButtonBase;
            bgButton.Click += new RoutedEventHandler(bgButton_Click);

            ButtonBase close = this.GetTemplateChild("close") as ButtonBase;
            close.Click += new RoutedEventHandler(close_Click);

            ButtonBase ok = this.GetTemplateChild("ok") as ButtonBase;
            ok.Click += new RoutedEventHandler(ok_Click);

            ButtonBase cancel = this.GetTemplateChild("cancel") as ButtonBase;
            cancel.Click += new RoutedEventHandler(cancel_Click);
        }

        void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Cancel();
        }

        void ok_Click(object sender, RoutedEventArgs e)
        {
            this.Commit();
        }

        void bgButton_Click(object sender, RoutedEventArgs e)
        {
            this.Cancel();
        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            this.Cancel();
        }

        private void Commit()
        {
            if (this.Content is DataForm)
            {
                var dataForm = this.Content as DataForm;

                if (dataForm.ValidateItem())
                {
                    dataForm.CommitEdit();
                    
                    this.Hide();

                    if (this.SubmitCommand != null)
                        this.SubmitCommand.Execute(null);
                }
            }
            else
            {
                this.Hide();
            }
        }

        private void Cancel()
        {
            if (this.Content is DataForm)
            {
                var dataForm = this.Content as DataForm;
                dataForm.CancelEdit();

                this.Hide();

                if (this.CancelCommand != null)
                    this.CancelCommand.Execute(null);
            }
           
        }

        public void Show()
        {
            this.IsShow = true;
        }
        
        public void Hide()
        {
            this.Focus();

            this.IsShow = false;
        }


        
    }
}
